function [likel,ssvec,flag_ok,lht,tempOut]=likelNanRegInitial(parest,parvec,parposest,funcmod,Y,trainvec,solveopt,addsol)
% =========================================================================
%% LikelNanRegInitial.m
%
% requires addsol.aZero addsol.pZero, initial state and MSE 
%
% This code estimates the likelihood of a 
% 1) NaN model 
% 2) Without Mixed Frequency 
% 3) Regular, i.e. NO Split in matrices
%
% [GG, RR, CONS, eu, SDX, ZZ, initss, ssvec, flag, ssnames]=feval(funcmod,param,options,addsol);
%
% =========================================================================
%% Initialize parameters
likel=-1e20; flag_ok=0;
parvec(parposest)=parest;
%% Model solution, second sample stored in structure second (second sample)
[G,R,C,eu,SDX,Z,structOne,ssvec,structTwo]=feval(funcmod,parvec,solveopt,addsol);
% A. Non-existence in either sample
if isequal(eu,[1;1])==0 
    return
end
[T,ny]=size(Y);
%% Demeaning 
if any(C~=0)==1;Y=Y-repmat((Z*C)',[T 1]);end;
Y=Y';

%% Initialization
ns=size(G,1);
if length(addsol.aZero)~=ns
    error('Initial state must be ns x 1')
end
if isequal( size(addsol.pZero) ,[ns ns])==false
    error('Initial covariance matrix must be [ns,ns]')
end

pshat=addsol.pZero;
shat =addsol.aZero; 
lht  = zeros(T,1);
Zdim =  zeros(T,1); 
mat_obspos = zeros(T,ny);
W = eye(ny);

% Not needed, use only for debugging 
% stt=zeros(ns,T+1); 
% ptt=zeros(ns,ns,T+1);  
% stt(:,1)=shat; 
% ptt(:,:,1)=pshat; 

% Filter
for ii=1:T;
    % Handling of missing observations 
    ytt=Y(:,ii);
    
    % Determine W and position of the NAN  
    ind =~isnan(ytt);
    rowt =find(~isnan(ytt));
    
    ytt=ytt(ind);    
    Zdim(ii)=length(ytt); 
    Ztt=W((ind==1),:)*Z;
    mat_obspos(ii,1:Zdim(ii))=rowt; 
    
    [shat,pshat,lht(ii)]=feval(@kf,ytt,Ztt,shat,pshat,G,R*(SDX'));
    
    % Not needed, use only for debugging 
    %     stt(:,ii+1)=shat;
    %     ptt(:,:,ii+1)=pshat;
    
end
%% Likelihood plus integration constant

lht   = lht(trainvec(1):trainvec(2));
likel = -0.5*log(2*pi)*(sum(Zdim))+sum(lht);
flag_ok=1;

%% End of File
end